<template>
  <cus-form ref="formRef" :nodes="nodes" width="420px" />
</template>

<script lang="ts">
import { ref } from 'vue'
import { useStore } from 'vuex';
import $ from '$';
import { ElMessage } from 'element-plus';

export default {
  props: {
    data: {
      type: Object as any,
      required: true
    }
  },
  setup(props) {
    let store = useStore();
    let userId = store.getters.userInfo.user.id;

    let formRef = ref();
    let nodes = ref<any[]>([
      {
        label: '练习名称',
        key: 'title',
        type: 'input',
        rule: { required: true, message: "请输入练习名称" },
        default: props.data.title
      },
      {
        label: "分类",
        key: "exerciseType",
        type: "select",
        options: [ { name: '进门测', id: 1 }, { name: '过手练', id: 2 }, { name: '出门测', id: 3 }, { name: '作业', id: 4 } ],
        rule: { required: true, message: "请选择分类" },
        default: props.data.exerciseType
      },
      {
        label: "学科",
        key: "subjectId",
        type: "cascader",
        url: "/permission/user/userDataSubjects",
        params: { userId },
        default: props.data.subjectId,
        rule: { required: true, message: "请选择学科" },
        valueKey: 'code',
        change: (v) => subjectChange(v)
      },
      {
        label: "年级",
        key: "gradeId",
        type: "select",
        options: [],
        default: props.data.gradeId,
        rule: { required: true, message: "请选择年级" },
      },
       {
        label: "教材",
        key: "teacherBookVersionId",
        type: "select",
        options: [],
        default: props.data.teacherBookVersionId,
        rule: { required: true, message: "请选择教材版本" },
      },
      {
        label: '班型',
        key: 'courseTypeId',
        type: 'select',
        default: props.data.courseTypeId,
        rule: { required: true, message: "请选择班型" },
      },
      {
        label: '总分',
        key: 'totalScore',
        type: 'number',
        default: props.data.totalScore,
        rule: { required: true, message: "请填写总分" },
      },
      {
        label: '学期',
        key: 'season',
        type: 'select',
        default: props.data.season,
        rule: { required: true, message: "请选择学期" },
      },
      {
        label: '年份',
        key: 'year',
        type: 'select',
        default: props.data.year,
        rule: { required: true, message: "请选择年份" },
      },
      {
        key: 'id',
        type: 'hidden',
        default: props.data.id
      }
    ]);
    $.http.post('/system/dictionary/queryDictByCodes', { typeCodesStr: 'QUES_SOURCE' }).then(res => nodes.value[4].options = res.json['QUES_SOURCE']);
    const subjectChange = (v, isInit?) => {
      $.http.post('/permission/user/userDataRules', { userId, subjectCode: v[1] }).then((res: any) => {
        nodes.value[3].options = res.json.grades;
        nodes.value[4].options = res.json.bookVersions;
        nodes.value[5].options = res.json.courseTypes;
        nodes.value[7].options = res.json.terms;
        nodes.value[8].options = res.json.years;
        if (!isInit) {
          formRef.value.formGroup.gradeId = null;
          formRef.value.formGroup.year = null;
          setTimeout(() => formRef.value.formRef.clearValidate() );
        }
      })
    }
    subjectChange([null, props.data.subjectId], true);

    const save = (resolve, reject) => {
      formRef.value.validate(async valid => {
        if (valid) {
          const res = await $.http.json('/tiku/paper/updatePaper', valid);
          if (res.result) {
            ElMessage.success('修改试卷成功~！');
            resolve(res)
          } else {
            ElMessage.warning('修改试卷失败！');
            reject();
          }
        }
      })
    }

    return { formRef, nodes, save }
  }
}
</script>

<style lang="scss" scoped>

</style>
